Skip to main content

JavaScript Code


Description

This action lets you run custom JavaScript code and do arithmetic operations with project variables.

Description


How to add it to your project?

Using the context menu: Add action → Your code → JavaScript.

How to add to project?


How does this action work?

There are two modes:

Locally

The code will run in an isolated environment, independent of the open app and outside of it.
You can use this method to work with any data JS supports—like:

  • variables
  • numbers
  • strings

Description

While working in this mode, you don’t need to write the return keyword if you plan to return a value. This action will return the result of the last line by itself. In the example above, variable {-Variable.result-} gets the value 6, which is the result of 2+2*2.

You can test this code with the JavaScript Tester.

Description

On the current page

The code will run on the open browser page. Use it when you want to work with the DOM tree and interact with page elements.

In this mode, you get access to all objects on the current page, including libraries and frameworks hooked up on the site (like jQuery).


No matter which mode you use

In the action settings, you must specify a variable to save the result, even if your code logic isn’t supposed to return anything.


Usage Examples

Arithmetic operations

Description
After this action, the result variable will contain the value of height divided by 2

Connecting JavaScript libraries

You can also embed a library that wasn’t on the page originally. For example, you can add jQuery with code like this:

int value1 = Convert.ToInt32(project.Variables["value1"].Value);
int value2 = Convert.ToInt32(project.Variables["value2"].Value);
int value3 = value1 + value2 // or value1 - value2 or value1 * value2, etc.
return value3.ToString(); // sum of two numbers